package com.zhangwei.zklx1015.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.zhangwei.zklx1015.entity.Apply;
import com.zhangwei.zklx1015.entity.ApplyVo;
import com.zhangwei.zklx1015.entity.Major;
import com.zhangwei.zklx1015.service.IApplyService;
import com.zhangwei.zklx1015.service.IMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangwei
 * @since 2021-10-15
 */
@Controller
@RequestMapping("/apply")
public class ApplyController {

    @Autowired
    private IApplyService iApplyService;

    @Autowired
    private IMajorService iMajorService;

    @RequestMapping("list")
    public String list(ApplyVo vo, Model model, @RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "3")Integer pageSize){

        PageInfo info = iApplyService.getlist(vo, pageNum, pageSize);


        model.addAttribute("vo",vo);
        model.addAttribute("info",info);


        return "list";
    }

    @RequestMapping("del")
    @ResponseBody
    public boolean del(Integer id){

        System.err.println("del====="+id);
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        boolean remove = iApplyService.remove(wrapper);
        if (remove){
            return  true;
        }
        return false;
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<Major> list = iMajorService.list();
        model.addAttribute("list",list);

        return  "add";
    }

    @RequestMapping("add")
    @ResponseBody
    public boolean add(Apply apply){

        System.err.println(apply);
        boolean save = iApplyService.save(apply);
        if (save){
            return  true;
        }

        return false;
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Model model,Integer id){

        System.err.println("toUpdate======"+id);

        Apply apply = iApplyService.selectApplyById(id);
        List<Major> list = iMajorService.list();
        model.addAttribute("list",list);

        model.addAttribute("apply",apply);

        return "update";
    }

    @RequestMapping("update")
    @ResponseBody
    public boolean update(Apply apply){

        System.out.println("修改"+apply);

        boolean save = iApplyService.saveOrUpdate(apply);
        if (save){
            return  true;
        }

        return false;
    }


    @RequestMapping("getMajor")
    @ResponseBody
    public List<Major> getMajor(){
        List<Major> list = iMajorService.list();
        return list;

    }
}

